package cn.achao.csr.chat.service.strategy.msg;


import cn.achao.csr.base.constant.enums.SysSourceEnum;
import cn.achao.csr.base.exception.BusinessException;
import cn.achao.csr.base.utils.ThrowUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 * Author: achao
 */
public class MsgHandlerFactory {
    private static final Map<Integer, AbstractMsgHandler> STRATEGY_MAP = new HashMap<>();

    public static void register(Integer code, AbstractMsgHandler strategy) {
        STRATEGY_MAP.put(code, strategy);
    }

    public static AbstractMsgHandler getStrategyNoNull(Integer code) {
        AbstractMsgHandler strategy = STRATEGY_MAP.get(code);
        ThrowUtils.throwIf(strategy == null, BusinessException.fail("消息类型不存在", SysSourceEnum.CSR_CHAT.getSysName()));
        return strategy;
    }
}
